package com.junmeng.libfloating.tab

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.junmeng.libfloating.base.FloatingManager
import com.junmeng.libfloating.ball.IPanelTab
import com.junmeng.libfloating.R

class TabMenu(private var builder: Builder) : IPanelTab {
    companion object {
        @JvmStatic
        fun newBuilder(): Builder {
            return Builder()
        }
    }

    class Builder() {

        var items: ArrayList<Item> = arrayListOf()
        fun registerItem(item: Item): Builder {
            this.items.add(item)
            return this
        }

        fun registerItems(items: List<Item>): Builder {
            this.items.addAll(items)
            return this
        }

        fun build(): TabMenu {
            return TabMenu(this)
        }
    }

    lateinit var recyclerView: RecyclerView
    lateinit var myAdapter: MyAdapter
    var maxSpanCount: Int = 5

    override fun getLayoutResId(): Int {
       return R.layout.floating_fragment_menus
    }

    override fun getLabel(): String {
        return "菜单"
    }

    override fun onInitView(view: View) {
        recyclerView = view.findViewById(R.id.floating_items)
        recyclerView.layoutManager = GridLayoutManager(FloatingManager.application, maxSpanCount)
        myAdapter = MyAdapter()
        recyclerView.adapter = myAdapter
        recyclerView.itemAnimator = DefaultItemAnimator()
        updateItems()
    }

    private fun updateItems() {
        myAdapter.datas.clear()
        myAdapter.datas.addAll(builder.items)
        if (myAdapter.itemCount > 0) {
            val gridLayoutManager = recyclerView.layoutManager as GridLayoutManager
            gridLayoutManager.spanCount = Math.min(myAdapter.itemCount, maxSpanCount)
        }
        myAdapter.notifyDataSetChanged()
    }


    inner class MyAdapter : RecyclerView.Adapter<MyAdapter.ViewHolder>() {
        var datas = mutableListOf<Item>()

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            return ViewHolder(
                LayoutInflater.from(parent.context)
                    .inflate(R.layout.floating_item_menu, parent, false)
            )
        }

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            val item = datas[position]
            holder.iconView.setImageResource(item.getIconResId())
            holder.labelView.text = item.getLabel()
            holder.itemView.setOnClickListener(item.getClickListener())
        }

        override fun getItemCount(): Int {
            return datas.size
        }

        inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            var iconView: ImageView = itemView.findViewById(R.id.icon)
            var labelView: TextView = itemView.findViewById(R.id.label)

        }
    }

    open class Item() {
        private var iconResId: Int = android.R.mipmap.sym_def_app_icon
        private var label: String = "标签"
        private var clickListener: View.OnClickListener? = null

        constructor(
            iconResId: Int = android.R.mipmap.sym_def_app_icon,
            label: String = "",
            clickListener: View.OnClickListener? = null
        ) : this() {
            this.label = label
            this.iconResId = iconResId
            this.clickListener = clickListener

        }

        open fun getIconResId(): Int {
            return iconResId
        }

        open fun getLabel(): String {
            return label
        }

        open fun getClickListener(): View.OnClickListener? {
            return clickListener
        }
    }
}